var nextPerson = {
    init: function () {
        this.layuiUse();
        this.tableReload();
        $('button[name="clickBtn"]').on('click', function () {
            nextPerson.checkPassword();
        });
    },
    layuiUse: function () {
        layui.use(['layer', 'form', 'jquery', 'table','laydate'], function () {
            var layer = layui.layer,
                form = layui.form,
                $ = layui.$,
                table = layui.table,
                laydate = layui.laydate;
                
                $('.layui-body').css('width',document.body.clientWidth-200);
        
        });
    },
    tableReload: function () {
        layui.use(['layer', 'form', 'jquery', 'table','element'], function () {
            var $ = layui.$,
                element = layui.element,
                table = layui.table;

                table.render({
                    elem: '#tableList',
                    method: 'post',
                    url: link + '/admin/user/list', //数据接口
                    where: {
                        token: GetCookie('teaAdminCheckPassword'),
                        superiorId:window.location.href.split('personId=')[1],
                    },
                    parseData: function (res) { //res 即为原始返回的数据
                        if (res.code == 10000) {
                            if(res.entity){
                                return {
                                    "code": res.code, //解析接口状态
                                    "msg": res.message, //解析提示文本
                                    "count": res.entity.total, //解析数据长度
                                    "data": res.entity.list //解析数据列表
                                };
                            }
                            
                        } else {
                            $('div[name="checkBox"]').removeClass('hide');
                            $('div[name="tableListBox"]').addClass('hide');
                        }
                    },
                    page: true, //开启分页
                    request: {
                        pageName: 'pageNum', //页码的参数名称，默认：page
                        limitName: 'limit' //每页数据量的参数名，默认：limit
                    },
                    response: {
                        statusName: 'code', //规定数据状态的字段名称，默认：code
                        statusCode: 10000, //规定成功的状态码，默认：0
                        msgName: 'msg', //规定状态信息的字段名称，默认：msg
                        countName: 'count', //规定数据总数的字段名称，默认：count
                        dataName: 'data', //规定数据列表的字段名称，默认：data
                    },
                    cols: [
                        [ //表头
                            {
                                field: 'mobile',
                                title: '手机号',
                                width:'12%',
                                templet: function (d) {
                                    return d.mobile;
                                }
                            },
                            {
                                field: 'nickname',
                                title: '用户名',
                                width:'12%',
                                templet: function (d) {
                                    return d.nickname;
                                }
                            },
                            {
                                field: 'totalExpenses',
                                title: '消费金额',
                                width:'12%',
                                templet: function (d) {
                                    return '￥'+d.totalExpenses;
                                }
                            },
                            {
                                field: 'sureBalance',
                                title: '可提现余额',
                                width:'12%',
                                templet: function (d) {
                                    return '￥'+d.sureBalance;
                                }
                            },
                            {
                                field: 'notBalance ',
                                title: '不可提现余额',
                                width:'12%',
                                templet: function (d) {
                                    return '￥'+d.notBalance ;
                                }
                            },
                            {
                                field: 'createDate',
                                title: '注册时间',
                                width:'15%',
                                templet: function (d) {
                                    return d.createDate;
                                }
                            },
                            {
                                field: 'set',
                                title: '操作',
                                templet: function (d) {
                                    return '<button class="layui-btn layui-btn-xs pl10 pr10 h30 line30" onclick="nextPerson.searchNextPerson('+d.id+')">查看下级用户</button>'+
                                       '<button class="layui-btn layui-btn-xs pl10 pr10 h30 line30" onclick="nextPerson.downLoad()">导出</button>';
                                }
                            },
                        ]
                    ],
                    done: function (res, curr, count) {
                        if (res.code == 10000) {
                            if(res.data){
                                $('.noNextPerson').addClass('hide');
                            }else{
                                $('.noNextPerson').removeClass('hide');
                            }
                            $('div[name="checkBox"]').addClass('hide');
                            $('div[name="tableListBox"]').removeClass('hide');
                        } else {
                            $('div[name="checkBox"]').removeClass('hide');
                            $('div[name="tableListBox"]').addClass('hide');
                        }
                    }
                });
        })

    },
    //判断是否需要输入动态口令
    checkPassword: function () {
        $.ajax({
            type: 'post',
            url: link + '/admin/checkToken',
            data: {
                'token': $('#pwd').val(),
            },
            dataType: 'json',
            success: function (res) {
                if (res.entity.flag) {
                    SetCookieDomain('teaAdminCheckPassword', res.entity.token, '');
                    $('div[name="checkBox"]').addClass('hide');
                    $('div[name="tableListBox"]').removeClass('hide');
                    location.reload();
                } else {
                    layui.use(['layer'], function () {
                        var layer = layui.layer;
                        layer.msg('动态口令输入错误！');
                    });
                }
            },
            error: function (res) {
                layui.use(['layer'], function () {
                    var layer = layui.layer;
                    layer.msg(res.message);
                });
            },
        });
    },
    searchNextPerson:function(res){
        window.location.href="../../../html/admin/nextPerson.html?personId="+res;
    },
    downLoad:function(){
        window.location.href = link + '/admin/exportExcel/userList?token=' + GetCookie('teaAdminCheckPassword')+'&superiorId='+window.location.href.split('personId=')[1];
    }
};
nextPerson.init();